// mypage/userCash/userCash.js
var util=require("../../utils/util")
var my=require("../../api/my")

Page({
  /*页面的初始数据*/
  data: {
    total_money: '0.00',
    cash_money: '',
    token: '',
    confirm: true
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    that.setData({token:wx.getStorageSync('token'),total_money:util.toDecimal(options.balance)})
  },
  input_money:function(e){
    var that=this,money=e.detail.value;
    that.setData({cash_money:money})

  },
  all_in:function(){
    var that=this;
    that.setData({cash_money:that.data.total_money})
  },
  confirm_cash:function(){
    var that=this,cash_money=that.data.cash_money,total_money=that.data.total_money,confirm=that.data.confirm;
    if(confirm){
     that.setData({confirm:false})
    }else{
      return false
    }
    if(!cash_money){
      util.page_show_toast("请输入提现金额",1500)
      return
    }
    if(cash_money>total_money){
      util.page_show_toast("超出可提现金额，请重新输入",1500)
      return
    }
    if(cash_money<1){
      util.page_show_toast('单笔提现最低金额不能低于1元',2000)
      return
    }
    if(cash_money>5000){
      util.page_show_toast('单笔提现最高金额不能超过5000元',2000)
      return
    }
    // 
    var params={
      withdrawAmount:cash_money,
      openId: wx.getStorageSync('openId')
    }
    my.walletCash(params,that.data.token).then(res=>{
      if(res.data.code==200){
        util.page_show_toast("提现成功，请等待系统审核",2000)
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        //
        prevPage.setData({renovate: true});
        setTimeout(()=>{
          wx.navigateBack({
            delta: 1
          });
        },1800)
      }else{
        that.setData({confirm:true})
        util.page_show_toast("提现失败，请稍后再试",2000)
      }
    })
  },
})